package com.neulogistics.core.customercenter.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackto.lib.mediat.core.IMediator;
import com.neulogistics.core.customercenter.api.application.command.ExchangeOrderCommand;
import com.neulogistics.core.customercenter.api.application.command.PlaceOrderCommand;
import com.neulogistics.core.customercenter.api.application.command.ReturnOrderCommand;
import com.neulogistics.core.customercenter.api.application.query.CustomerQuery;
import com.neulogistics.core.customercenter.api.application.query.IOrderQueryService;
import com.neulogistics.core.customercenter.api.application.query.OrderQuery;
import com.neulogistics.core.customercenter.api.application.viewmodel.CustomerViewModel;
import com.neulogistics.core.customercenter.api.application.viewmodel.OrderViewModel;
import com.neulogistics.core.customercenter.domain.repository.IOrderRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@RestController
@Api(tags = "订单管理")
@RequestMapping("/customerCenter/order")
public class OrderController {
    @Resource
    IMediator mediator;

    @Resource
    IOrderRepository orderRepository;

    @Resource
    IOrderQueryService orderQueryService;

    @ApiOperation("根据id查询订单信息")
    @GetMapping("/get/{id}")
    public BaseResponse<OrderViewModel> get(@PathVariable @Positive @NotNull Long id){
        // 已校验数据格式
        // 查询

        OrderViewModel orderViewModel = orderQueryService.getOrderById(id);

        return ResultUtils.success(orderViewModel);
    }


    @ApiOperation("下单")
    @PostMapping("/place")
    public BaseResponse<Long> placeOrder(@RequestBody @Validated @NotNull PlaceOrderCommand command){
        // 已校验数据格式

        BaseResponse<Long> baseResponse = mediator.send(command);
        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("退货")
    @PostMapping("/returnOrder")
    public BaseResponse<Long> returnOrder(@RequestBody @Validated @NotNull ReturnOrderCommand command){
        // 已校验数据格式

        BaseResponse<Long> baseResponse = mediator.send(command);
        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("换货")
    @PostMapping("/exchangeOrder")
    public BaseResponse<Long> exchangeOrder(@RequestBody @Validated @NotNull ExchangeOrderCommand command){
        // 已校验数据格式

        BaseResponse<Long> baseResponse = mediator.send(command);
        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("根据条件分页查询订单信息")
    @PostMapping("/page")
    public BaseResponse<Page<OrderViewModel>> getCustomerByPage(@RequestBody @Validated @NotNull OrderQuery orderQuery){
        // 已校验数据格式
        // 查询
        Page<OrderViewModel> page = orderQueryService.page(orderQuery);
        // 修改成功，返回
        return ResultUtils.success(page);
    }
}
